package com.gopay.scheduler.mail;

import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Map;

/**
 * 邮件发送引擎
 * 
 * @MailEngine.java
 * @author yaobo
 * @2013-1-23 下午03:35:59 www.gopay.com.cn Inc.All rights reserved.
 */
public class MailEngine {
    @Resource(name = "mailSender")
    private JavaMailSender mailSender;

    @Resource(name = "velocityEngine")
    private VelocityEngine velocityEngine;

    /**
     * 邮件模板前缀
     */
    private final static String VM_PREFIX = "mail/";

    /**
     * 邮件模板后缀
     */
    private final static String VM_SUFFIX = ".vm";

    /**
     * 邮件编码
     */
    private String mailEncoding = "UTF-8";

    /**
     * 模板路径
     */
    /* 多任务情况下会有问题，会取错模板 */
//    private String vmName;

    /**
     * 邮件标题
     */
    private String subject;

    /**
     * 发件人
     */
    private String sender;

    /**
     * 发送邮件
     * 
     * @param receivers
     *            收件人数组
     * @param html
     *            是否以HTML形式展示
     * @param vmName
     *            邮件模板名称
     * @param model
     *            数据集
     * @throws MessagingException
     */
    public void send(String[] receivers, boolean html, String vmName, Map<String, Object> model)
            throws Exception {
        String vmFileName = VM_PREFIX + vmName + VM_SUFFIX;
        send(receivers, subject, html, vmFileName, model);
    }

    /**
     * 发送邮件
     * 
     * @param receivers
     *            收件人数组
     * @param mailSubject
     *            邮件标题
     * @param html
     *            是否以HTML形式展示
     * @param model
     *            数据集
     * @throws MessagingException
     */
    public void send(String[] receivers, String mailSubject, boolean html, String vmName, Map<String, Object> model)
            throws Exception {

        String vmFileName = VM_PREFIX + vmName + VM_SUFFIX;

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messagehelper = new MimeMessageHelper(mimeMessage, true, mailEncoding);

        // 设置VM页面进行渲染
        String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, vmFileName, mailEncoding, model);
        messagehelper.setFrom(sender);
        messagehelper.setTo(receivers);
        messagehelper.setSubject(subject);
        if (StringUtils.isNotBlank(mailSubject)) {
            messagehelper.setSubject(mailSubject);
        }
        messagehelper.setText(content, html);

        mailSender.send(mimeMessage);
    }

//    public void setVmName(String vmName) {
//        this.vmName = vmName;
//    }

    public void setMailEncoding(String mailEncoding) {
        this.mailEncoding = mailEncoding;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }
}
